package com.infonautics.action;

import java.io.File;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Random;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.SessionAware;

import com.hibernate.Company;
import com.hibernate.Personalinfo;
import com.hibernate.Userinfo;
import com.infonautics.dao.ICompanyDao;
import com.infonautics.dao.IPersonalinfoDao;
import com.infonautics.dao.imp.ICompanyDaoImp;
import com.infonautics.dao.imp.IPersonalinfoDaoImp;
import com.infonautics.page.PageBean;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.Preparable;
import com.system.dao.ISystemDao;

public class PersonalinfoAction extends ActionSupport implements Preparable,SessionAware{
	private static final long serialVersionUID = 1L;
	private Map<String,Object> session;
	private String pageUrl = "/WEB-INF/personalinfo/";
	private String successMsg;
	private String url;
	private String errormessage;
	private Userinfo userinfo;
	private ISystemDao<Personalinfo> personinfoDao = new IPersonalinfoDaoImp();
	private IPersonalinfoDao perinfoDao = new  IPersonalinfoDaoImp();
	private ICompanyDao comDao = new  ICompanyDaoImp();
	private Personalinfo perinfo;
	private PageBean<Personalinfo> pageBean;
	private int page = 1;
	private int id;
	private String perinfoname;
	private List<Company> companyList;
	//图片上传
	private File upload; 
	private String uploadFileName;
    private String dir; 
    private String targetFileName;
    private String urls;
    private String uploadDir;
    private String path="";
    private String pathUrl;
    private String uploadname;
    private String upname="";
	
	@Override
	public void prepare() throws Exception {
		if(session.containsKey("userinfo")){
			if(session.get("userinfo")!=null){
				userinfo=(Userinfo)session.get("userinfo");
			}
		}
		
	}
	
	//查询分公司人员信息列表
	public String list() throws Exception{
		pageBean = perinfoDao.doPersonalinfoList(20, page);
		return "input";
	}

	public String query() throws Exception{
		String queryString = "";
		if(perinfoname !=null && !perinfoname.equals("")){
			perinfoname=new String(perinfoname.getBytes("ISO8859-1"),"UTF-8");
			queryString += "and name like '%" + perinfoname + "%'";
		}
		pageBean = perinfoDao.doQueryList(20, page, queryString);
		pageBean.setQueryString(queryString);
		return "list";
	}
	//新增分公司人员页面
	public String addPage()throws Exception{
		companyList = comDao.doCompanyAll();
		return "input";
	}
	
	//新增分公司人员
	public String add() throws Exception{
		if(upload != null){
			// 获得upload路径的实际目录 
	        String realPath = ServletActionContext.getServletContext().getRealPath(uploadDir); 
	        //获得实际目录 
	        String targetDirectory = realPath; 
	        String mydir = ""; 
	        String tname = ""; 
	        String url = "";
        	// 生成保存文件的文件名称 
	        tname = generateFileName(uploadFileName); 
	        // 保存文件的路径 
	        mydir = targetDirectory + "/" + tname; 
	        url = uploadDir + "/" + tname;
	        upname+=uploadFileName+"|";
	        setUploadname(upname);//文件中文名
	        path+= url+"|";
	        setPathUrl(path);//文件路径
	        // 建立一个目标文件 
	        File target = new File(targetDirectory, tname); 
	        // 将临时文件复制到目标文件 
	        FileUtils.copyFile(upload, target);
	        setDir(mydir); 
	        setTargetFileName(tname); 
	        setUrls(url);
	        String path_1 = pathUrl.substring(0,pathUrl.lastIndexOf("|"));
	        perinfo.setPhoto(path_1);
		}
		personinfoDao.doAdd(perinfo);
		doMsgHandle("添加成功","Personalinfo_list.action");
		return PersonalinfoAction.SUCCESS;
	}
	
	//修改分公司人员页面
	public String modifyPage() throws Exception{
		perinfo = personinfoDao.doOne(id);
		return "input";
	}
	//修改分公司人员信息
	public String modify() throws Exception{
		personinfoDao.doModfiy(perinfo);
		doMsgHandle("修改成功","Company_list.action");
		return PersonalinfoAction.SUCCESS;
		
	}
	//删除分公司人员
	public String delete() throws Exception{
		personinfoDao.doDelete(id);
		doMsgHandle("删除成功","Company_list.action");
		return PersonalinfoAction.SUCCESS;
	}
	// 为上传文件自动分配文件名称，避免重复 
    private String generateFileName(String fileName) { 
        // 获得当前时间 
        DateFormat format = new SimpleDateFormat("yyMMddHHmmss"); 
        // 转换为字符串 
        String formatDate = format.format(new Date()); 
        // 随机生成文件编号 
        int random = new Random().nextInt(10000); 
        // 获得文件后缀名称 
        int position = fileName.lastIndexOf("."); 
        String extension = fileName.substring(position); 
        // 组成一个新的文件名称 
       return formatDate + random + extension; 
    }
	//服务方法
	private void doMsgHandle(String successMsg,String url){
		this.successMsg=successMsg;
		this.url=url;
	}
	
	public Map<String, Object> getSession() {
		return session;
	}
	
	public void setSession(Map<String, Object> session) {
		this.session = session;
	}
	
	public String getErrormessage() {
		return errormessage;
	}
	
	public void setErrormessage(String errormessage) {
		this.errormessage = errormessage;
	}
	
	public String getPageUrl() {
		return pageUrl;
	}

	public void setPageUrl(String pageUrl) {
		this.pageUrl = pageUrl;
	}

	public Userinfo getUserinfo() {
		return userinfo;
	}
	
	public void setUserinfo(Userinfo userinfo) {
		this.userinfo = userinfo;
	}
	
	public String getSuccessMsg() {
		return successMsg;
	}
	
	public void setSuccessMsg(String successMsg) {
		this.successMsg = successMsg;
	}
	
	public String getUrl() {
		return url;
	}
	
	public void setUrl(String url) {
		this.url = url;
	}

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	
	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public Personalinfo getPerinfo() {
		return perinfo;
	}

	public void setPerinfo(Personalinfo perinfo) {
		this.perinfo = perinfo;
	}

	public PageBean<Personalinfo> getPageBean() {
		return pageBean;
	}

	public void setPageBean(PageBean<Personalinfo> pageBean) {
		this.pageBean = pageBean;
	}

	public String getPerinfoname() {
		return perinfoname;
	}

	public void setPerinfoname(String perinfoname) {
		this.perinfoname = perinfoname;
	}

	public List<Company> getCompanyList() {
		return companyList;
	}

	public void setCompanyList(List<Company> companyList) {
		this.companyList = companyList;
	}

	public File getUpload() {
		return upload;
	}

	public void setUpload(File upload) {
		this.upload = upload;
	}

	public String getUploadFileName() {
		return uploadFileName;
	}

	public void setUploadFileName(String uploadFileName) {
		this.uploadFileName = uploadFileName;
	}

	public String getDir() {
		return dir;
	}

	public void setDir(String dir) {
		this.dir = dir;
	}

	public String getTargetFileName() {
		return targetFileName;
	}

	public void setTargetFileName(String targetFileName) {
		this.targetFileName = targetFileName;
	}

	public String getUrls() {
		return urls;
	}

	public void setUrls(String urls) {
		this.urls = urls;
	}

	public String getUploadDir() {
		return uploadDir;
	}

	public void setUploadDir(String uploadDir) {
		this.uploadDir = uploadDir;
	}

	public String getPath() {
		return path;
	}

	public void setPath(String path) {
		this.path = path;
	}

	public String getPathUrl() {
		return pathUrl;
	}

	public void setPathUrl(String pathUrl) {
		this.pathUrl = pathUrl;
	}

	public String getUploadname() {
		return uploadname;
	}

	public void setUploadname(String uploadname) {
		this.uploadname = uploadname;
	}

	public String getUpname() {
		return upname;
	}

	public void setUpname(String upname) {
		this.upname = upname;
	}

	
	
	
}
